home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power Programmierung
/
Power-Programmierung (Tewi)(1994).iso
/
magazine
/
nvdc87
/
lcv1n1
/
mean-c.c
< prev
next >
Wrap
Text File
|
1987-08-12
|
916b
|
34 lines
/* Listing 3 - C function called by Turbo Prolog to find the mean.*/
struct ilist { /* Declare a Turbo Prolog list in C */
char functor;
double val;
struct ilist *next;
};
struct ifunc { /* Declare a Turbo Prolog functor in C */
char type;
double value;
};
void mean_0(struct ilist *in, struct ifunc **out) {
int count = 0;
double y = 0, z = 0;
if (in->functor !=1)
fail_cc(); /* 1 indicates a list element. */
while(in->functor !=2) { /* 2 indicates an empty list. */
y = y + in->val; /* Keep a running sum of the list. */
count = count+1;
in = in->next; /* Get the next member of the list. */
}
z = y/count; /* z = the mean. */
*out = (struct ifunc *) palloc (sizeof(struct ifunc));
(*out)->value = z;
(*out)->type = 1;
}